<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 6/28/21
 * Time: 12:19 AM
 */

namespace app\api\controller\v1;


use app\api\model\ProductOrder;
use app\api\model\StoreUser as StoreUserModel;
use app\api\model\TeamUser as TeamUserModel;
use app\api\model\User as UserModel;
use app\api\model\UserInvitation;

class StoreUserRank
{
    // 员工排行
    public function getStoreUserList($start_time,$end_time)
    {
        $where = [];
        if (!$start_time) {
            $start_time = strtotime("-1 month");
            $end_time = time();
            $where[] = ['pay_time', 'egt', $start_time];
            $where[] = ['pay_time', 'elt', $end_time];
        } else {
            $where[] = ['pay_time', 'egt', strtotime($start_time." 00:00:00")];
            $where[] = ['pay_time', 'elt', strtotime($end_time." 23:59:59")];
        }
        $storeUser = StoreUserModel::with('store')->select()->toArray();
        foreach ($storeUser as $k => $v) {
            $whereTwo = [];
            $whereTwo[] = ['inviter', 'eq', $v['id']];
            $whereTwo[] = ['type', 'eq', 2];
            $userInvitation = UserInvitation::where($whereTwo)->select()->toArray();
            $ids = array_column($userInvitation, 'invitee');
            $whereThree = $where;
            $whereThree[] = ['user_id', 'in', $ids];
            $whereThree[] = ['status', 'gt', 1];
            $store_user_count = ProductOrder::where($whereThree)
                ->sum('actual_price');
            $whereFour = $where;
            $whereFour[] = ['user_id', 'eq', $v['user_id']];
            $whereFour[] = ['status', 'gt', 1];
            $store_user_count_one = ProductOrder::where($whereFour)
                ->where('status', 'gt', 1)->sum('actual_price');
            $storeUser[$k]['count'] = $store_user_count + $store_user_count_one;
        }
        if(count($storeUser) > 0) {
            $storeUser = arraySort($storeUser,'count');
        }
        return $storeUser;
    }

    // 查询订单
    public function getStoreUserOrder($start_time,$end_time,$store_user_id)
    {
        $where = [];
        if (!$start_time) {
            $start_time = strtotime("-1 month");
            $end_time = time();
            $where[] = ['pay_time', 'egt', $start_time];
            $where[] = ['pay_time', 'elt', $end_time];
        } else {
            $where[] = ['pay_time', 'egt', strtotime($start_time." 00:00:00")];
            $where[] = ['pay_time', 'elt', strtotime($end_time." 23:59:59")];
        }
        $store_user = StoreUserModel::get($store_user_id);
        $whereTwo = [];
        $whereTwo[] = ['inviter', 'eq', $store_user_id];
        $whereTwo[] = ['type', 'eq', 2];
        $userInvitation = UserInvitation::where($whereTwo)->select()->toArray();
        $ids = array_column($userInvitation, 'invitee');
        $whereThree = $where;
        $whereThree[] = ['user_id', 'in', $ids];
        $whereThree[] = ['status', 'gt', 1];
        $store_user_list = ProductOrder::where($whereThree)
            ->with('store,user,content')
            ->select();
        $array = [];
        if(!$store_user_list->isEmpty()) {
            foreach ($store_user_list->toArray() as $k=>$v){
                $invite = UserInvitation::where('invitee',$store_user_list[$k]['user_id'])->find();
                if($invite['type'] == 1) {
                    $store_user_list[$k]['invite'] = UserModel::get($invite['inviter']);
                }elseif($invite['type'] == 2){
                    $store_user = StoreUserModel::get($invite['inviter']);
                    $store_user_list[$k]['invite'] = UserModel::get($store_user['user_id']);
                    $store_user_list[$k]['invite']['nickname'] = $store_user['name'];
                    $store_user_list[$k]['invite']['phone'] = $store_user['phone'];
                }elseif($invite['type'] == 3){
                    $team_user = TeamUserModel::get($invite['inviter']);
                    $store_user_list[$k]['invite'] = UserModel::get($team_user['user_id']);
                    $store_user_list[$k]['invite']['nickname'] = $team_user['name'];
                    $store_user_list[$k]['invite']['phone'] = $team_user['phone'];
                }
                $store_user_list[$k]['pay_time_int'] = strtotime($store_user_list[$k]['pay_time']);
                array_push($array,$store_user_list[$k]);
            }
        }
        $whereFour = $where;
        $whereFour[] = ['user_id', 'eq', $store_user['user_id']];
        $whereFour[] = ['status', 'gt', 1];
        $list = ProductOrder::where($whereFour)
            ->with('store,user,content')
            ->where('status', 'gt', 1)
            ->select();
        if(!$list->isEmpty()) {
            foreach ($list->toArray() as $k=>$v){
                $invite = UserInvitation::where('invitee',$list[$k]['user_id'])->find();
                if($invite['type'] == 1) {
                    $list[$k]['invite'] = UserModel::get($invite['inviter']);
                }elseif($invite['type'] == 2){
                    $store_user = StoreUserModel::get($invite['inviter']);
                    $list[$k]['invite'] = UserModel::get($store_user['user_id']);
                    $list[$k]['invite']['nickname'] = $store_user['name'];
                    $list[$k]['invite']['phone'] = $store_user['phone'];
                }elseif($invite['type'] == 3){
                    $team_user = TeamUserModel::get($invite['inviter']);
                    $list[$k]['invite'] = UserModel::get($team_user['user_id']);
                    $list[$k]['invite']['nickname'] = $team_user['name'];
                    $list[$k]['invite']['phone'] = $team_user['phone'];
                }
                $list[$k]['pay_time_int'] = strtotime($list[$k]['pay_time']);
                array_push($array,$store_user_list[$k]);
            }
        }
        if(count($array) > 0) {
            $array = arraySort($array,'pay_time_int');
        }
        return $array;
    }
}